var prefix = api + "/admin/dept";
var oper = getQueryString('oper')
var deptId = getQueryString('id')
$(function () {
	$('#oper').val(oper)
	if (oper === 'edit') {
		deptDetail(deptId)
	}

	jQuery.validator.addMethod("format",function(value,element){
		var account = /^[A-Z_]+$/;
		return account.test(value);
	},"请输入大写英文、下划线");
})

function deptDetail(id){
	$.operate.get(api + '/admin/dept/' + id, function (res) {
		res.data.oper = oper
		res.data.parentName = res.data.parentName || '无'
		applyDrawing('#editForm',res.data)
	}, false)
}

function selectDeptTree() {
	if($("#oper").val()=='add'){
		var deptId = $("#treeId").val();
		var url = "/pages/sysadm/dept/tree?deptId=" + deptId;
		var options = {
			title: '部门选择',
			width: "380",
			url: url,
			callBack: doSubmit
		};
		$.modal.openOptions(options);
	}
}

function doSubmit(index, layero){
	var body = layer.getChildFrame('body', index);
	$("#treeId").val(body.find('#treeId').val());
	$("#treeName").val(body.find('#treeName').val());
	layer.close(index);
}
$(document).on('focus', '#treeName',function(){
	selectDeptTree()
})
$("#editForm").validate({
	onkeyup: false,
	rules:{
		name:{
			required:true
		},
		deptCode:{
			required:true,
			format: true
		},
		sort:{
			digits:true
		},
	},
    focusCleanup: true
});

function submitHandler() {
    if ($.validate.form()) {
    	$.common.ajaxCommitForm($('#editForm'),false);
    	return false;
    }
}